package com.immediatus.graphics.managers;

import com.immediatus.graphics.font.Font;
import com.immediatus.graphics.font.FontTable;

import javax.microedition.khronos.opengles.GL10;
import java.util.ArrayList;

public class FontManager{
    private final ArrayList<Font> mFontsManaged = new ArrayList<Font>();

    public synchronized void clear(){
        this.mFontsManaged.clear();
    }

    public synchronized void loadFont(final Font font_){
        if (font_ == null){
            throw new IllegalArgumentException("font_ must not be null!");
        }
        this.mFontsManaged.add(font_);
    }

    public synchronized void loadFonts(final FontTable fontLibrary_){
        fontLibrary_.loadFonts(this);
    }

    public void loadFonts(final Font... fonts_){
        for (int i = fonts_.length - 1; i >= 0; i--){
            this.loadFont(fonts_[i]);
        }
    }

    public synchronized void updateFonts(final GL10 gl_){
        final ArrayList<Font> fonts = this.mFontsManaged;
        final int fontCount = fonts.size();
        if (fontCount > 0){
            for (int i = fontCount - 1; i >= 0; i--){
                fonts.get(i).update(gl_);
            }
        }
    }

    public synchronized void reloadFonts(){
        final ArrayList<Font> managedFonts = this.mFontsManaged;
        for (int i = managedFonts.size() - 1; i >= 0; i--){
            managedFonts.get(i).reload();
        }
    }
}
